package com.module.lemlin.owner

import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorRes
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ToastUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class OwnerAbstractFragment : Fragment(), View.OnTouchListener {

    private var isFirstLoad = false

    /**
     * 需要绑定的视图ID
     */
    abstract val layoutResId: Int

    private var rootView: View? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return rootView ?: inflater.inflate(layoutResId, container, (false))
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (!EventBus.getDefault().isRegistered((this))) {
            EventBus.getDefault().register((this))
        }
        view.setOnTouchListener(this)
        doBeforeInit()
        initView()
    }

    override fun onResume() {
        super.onResume()
        if (!isFirstLoad) {
            initData()
            isFirstLoad = true
        } else {
            onRefreshLoad()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister((this))
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventMainThread(any: Any) {
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        return true
    }

    /**
     * 设置沉浸式状态栏
     * */
    private fun initStatusBar() {

        //设置状态栏模式
        BarUtils.setStatusBarLightMode(
            requireActivity(), stateBar().isLightMode
        )
        //设置状态栏颜色
        BarUtils.setStatusBarColor(
            requireActivity(), ColorUtils.getColor(stateBar().color)
        )
        //为标题栏添加状态栏的高度
        stateBar().view?.let {
            BarUtils.addMarginTopEqualStatusBarHeight(it)
        }
        //设置底部菜单栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            BarUtils.setNavBarColor(
                requireActivity().window,
                ColorUtils.getColor(stateBar().bottomColor)
            )
        }
    }


    /**
     * 在初始化视图之前的操作
     */
    open fun doBeforeInit() = Unit

    /**
     * 初始化UI
     */
    open fun initView() = Unit

    /**
     * 初始化数据
     */
    open fun initData() = Unit


    /**
     * 刷新数据
     */
    open fun onRefreshLoad() = Unit

    /**
     * toast提示
     * */
    fun toast(message: String?) {
        ToastUtils.showShort(message ?: "未知错误")
    }

    /**
     *
     * */
    open fun stateBar(): StatusBar = StatusBar()

    data class StatusBar(
        var isLightMode: Boolean = true,
        @ColorRes var color: Int = android.R.color.transparent,
        @ColorRes var bottomColor: Int = android.R.color.white,
        var view: View? = null
    )
}